package com.yule.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class BaseController {

    @InitBinder
    public void initBinder(WebDataBinder binder){
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); //时间格式
        binder.registerCustomEditor(Date.class, "begintime", new CustomDateEditor(timeFormat, false));
        binder.registerCustomEditor(Date.class, "endtime", new CustomDateEditor(timeFormat, false));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //日期格式
        binder.registerCustomEditor(Date.class, "birthday", new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(Date.class, "startDate", new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(Date.class, "finishDate", new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(Date.class, "planDate", new CustomDateEditor(dateFormat, true));
    }

    public void fileCopy(File source, File target) {
        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(source), 1024);
                out = new BufferedOutputStream(new FileOutputStream(target), 1024);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void createFolder(String path) {
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdir();
        }
    }

    protected void download(File file, String name, HttpServletResponse response) throws IOException {
        if (StringUtils.isEmpty(name)) {
            name = file.getName();
        }
        OutputStream os = response.getOutputStream();
        FileInputStream fis = new FileInputStream(file);
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("UTF-8"), "iso-8859-1"));
        response.setHeader("Content-Length", String.valueOf(file.length()));
        byte[] b = new byte[8192];
        int n = 0;
        while ((n = fis.read(b)) != -1) {
            os.write(b, 0, n);
        }
        fis.close();
        os.flush();
        os.close();
    }

    protected String getStaticPath() {
    	HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
    	String realPath = new ApplicationHome(getClass()).getSource().getParentFile().getPath();
    	//return realPath.substring(0, realPath.lastIndexOf("\\cnmar-cloud\\")) + "/cnmar-cloud/static/";
    	return realPath;
    }
}
